<?php

namespace app\common\repositories\user;


use app\common\dao\BaseDao;
use app\common\dao\user\UserIntegralConfigDao;
use app\common\repositories\BaseRepository;
use app\common\repositories\store\order\StoreOrderRepository;
use app\common\repositories\store\product\ProductRepository;
use crmeb\jobs\SendSmsJob;
use crmeb\services\RedisCacheService;
use think\facade\Queue;
use think\Model;

class UserIntegralConfigRepository extends BaseRepository
{
    /**
     * @var UserIntegralConfigDao
     */
    protected $dao;

    /**
     * UserIntegralConfigRepository constructor.
     * @param UserIntegralConfigDao $dao
     */
    public function __construct(UserIntegralConfigDao $dao)
    {
        $this->dao = $dao;
    }

    public function adminList()
    {
        $list = $this->dao->search([])->select()->toArray();
        $config = [
            '购买任务'=>['evaluate','buy_product','buy_seckill_product'],
            '活跃任务'=>['browse_product','seckill_product','sign','watch_video','browse_ad','share','sign_ew','integral_user_give'],
            '基本任务'=>['perfect_user_info','bind_phone'],
        ];
        $arr = [];
        foreach ($config as $key => $item){
            $arr1 =[];

            foreach ($list as $vs){
                if(in_array($vs['category'],$item)){
                    array_push($arr1,$vs);
                }
            }
            $arr[$key]= $arr1;
        }
        return compact('arr');
    }

    public function apiList($userInfo)
    {
        $list = $this->dao->search(['status'=>1])->select()->toArray();
        $config = [
            '购买任务'=>['evaluate','buy_product','buy_seckill_product'],
            '活跃任务'=>['browse_product','seckill_product','sign','watch_video','browse_ad','share','sign_ew','integral_user_give'],
            '基本任务'=>['perfect_user_info','bind_phone'],
        ];
        $arr = [];
        foreach ($config as $key => $item){
            $arr1 =[];
            $arr1['title'] = $key;
            $arr1['children'] =[];
            foreach ($list as &$vs){
                if(in_array($vs['category'],$item)){
                    $vs['num1'] = 0;
                    $vs['is_complete'] = 0;
                    if(in_array($vs['category'],['buy_seckill_product','watch_video','share','seckill_product','browse_product','browse_ad'])){
                        $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','date'=>'today','type' => $vs['category'],'uid' => $userInfo->uid]);
                        if($num >= $vs['num']){
                            $vs['is_complete'] = 1;
                            $vs['num1'] = $vs['num'];
                        }else{
                            $vs['num1'] =$num;
                        }
                    }
                    if(in_array($vs['category'],['bind_phone'])){
                        $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','type' => $vs['category'],'uid' => $userInfo->uid]);
                        if($num > 0 || $userInfo->phone){
                            $vs['is_complete'] = 1;
                        }
                    }
                    if(in_array($vs['category'],['sign_ew','buy_product'])){
                        $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','date'=>'today','type' => $vs['category'],'uid' => $userInfo->uid]);
                        if($num > 0){
                            $vs['is_complete'] = 1;
                        }
                    }
                    if(in_array($vs['category'],['perfect_user_info'])){
                        $user_info_time = systemConfig('user_info_time');
                        $num = app()->make(UserIntegralBillRepository::class)->getCount(['category' => 'integral','start_time'=>$user_info_time,'type' => $vs['category'],'uid' => $userInfo->uid]);
                        if($num > 0){
                            $vs['is_complete'] = 1;
                        }
                    }
                    array_push($arr1['children'],$vs);
                }
            }
            $arr[]= $arr1;
        }
        return $arr;
    }

    public function getConfig($name)
    {
        return $this->dao->getWhere(['category'=>$name]);
    }
}
